package com.doujiang.sys.service.impl;
import com.doujiang.sys.constant.SysConstant;
import com.doujiang.sys.domain.Uservo;
import com.doujiang.sys.utils.DataGridView;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.doujiang.sys.domain.User;
import com.doujiang.sys.mapper.UserMapper;
import com.doujiang.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：豆浆
 * @name :UserServiceImpl
 * @Date：2023/12/18 20:41
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    /*
     *用户登录的核心方法
     * */
    @Override
    public User login(Uservo uservo) {

        //明⽂
        //⽣成密⽂
        //根据用户名和密码查询
        //先uservo中用户输入的密码进行md5加密操作
        String pwd = DigestUtils.md5DigestAsHex(uservo.getPwd().getBytes());
        uservo.setPwd(pwd);
        //调用userMapper接口中的查询方法
        return userMapper.login(uservo);
    }

    //查询用户 分⻚查询pagehelper
    @Override
    public DataGridView queryAllUser(Uservo userVo) {
        Page<Object> page = PageHelper.startPage(userVo.getPage(), userVo.getLimit());
        List<User> list = userMapper.queryAllUser(userVo);
        return new DataGridView(page.getTotal(),list);
    }

    //添加用户
    @Override
    public void addUser(Uservo userVo) {
        //设置默认密码 md5
        userVo.setPwd(DigestUtils.md5DigestAsHex(SysConstant.USER_DEFAULT_PWD.getBytes()));
        //设置用户类型都是普通用户type=2
        userVo.setType(SysConstant.USER_TYPE_NORMAL);
        this.userMapper.insertSelective(userVo);
    }

    //修改用户
    @Override
    public void updateUser(Uservo userVo) {
        userMapper.updateByPrimaryKeySelective(userVo);
    }

    @Override
    public void deleteUser(Integer userid) {
        //删除用户
        userMapper.deleteByPrimaryKey(userid);
    }

    //批量删除用户
    @Override
    public void deleteBatchUser(Uservo userVo) {
        Integer[] ids = userVo.getIds();
        for (Integer id : ids){
            deleteUser(id);
        }
    }

    //重置密码
    @Override
    public void resetUserPwd(Integer userid) {
        Uservo userVo = new Uservo();
        userVo.setUserid(userid);
        //设置默认的密码,加密操作
        userVo.setPwd(DigestUtils.md5DigestAsHex(SysConstant.USER_DEFAULT_PWD.getBytes()));
        //更新操作
        userMapper.updateByPrimaryKeySelective(userVo);

    }

}
